<?php

/**
 * 职位控制器
 * User: wangchao
 * Date: 2017/10/17 0017
 * Time: 8:19
 */

namespace Admin\Controller;

class PositionController extends CommonController
{
    /**
     * 上传图片
     */
    public function doUploadPic()//上传模块
    {
        $upload = new \Think\Upload();
        $upload->maxSize = 3145728;
        $upload->exts = array('jpg', 'gif', 'png', 'jpeg');
        $upload->rootPath = './Uploads/'; // 设置附件上传根目录
        $upload->savePath = 'pc/'; // 设置附件上传子目录
        $info = $upload->upload();
        if(!$info){
            $this->error($upload->getError());
        }else{
            foreach($info as $file){
            $data = 'Uploads/'.$file['savepath'] . $file['savename'];
            $file_a=$data;
            echo '{"code":0,"msg":"成功上传","data":{"src":"'.$file_a.'"}}';
            }
        }
    }
    /**
     * 公司列表
     */
    public function company_list()
    {
        $admin_id = session('admin_id');
        $user_id = session('user_id');

        $user = M('user')->where(['user_id'=>$user_id])->find();
        if(empty($user_id)){
            $count = M('company')->where( 'status = 1')->order('id desc')->count();
        }else{
            $count = M('company')->where( 'status = 1')->order('id desc')->where(array("id"=>array("in", $user['company_id'])))->count();
        }
        
        $page = getpage($count,10);
        $show = $page->show();
        if(empty($user_id)){
            $list = M('company')->where('status=1')->limit($page->firstRow.','.$page->listRows)->order('id desc')->select();
        }else{
            $list = M('company')->where('status=1')->where(array("id"=>array("in", $user['company_id'])))->limit($page->firstRow.','.$page->listRows)->order('id desc')->select();
        }
        
        $type = M('type_list')->select();
        $this->assign('list', $list);
        $this->assign('count',$count);
        $this->assign('page',$show);
        $this->assign('type',$type);
        $this->display('company_list');
    }
    //添加企业坐标
    public function addzb(){
        $zuobiao = I('zuobiao');
        $gps = M('company')->where(['gps_position'=>$zuobiao])->find();
        if ($zuobiao) {
            $zb = $zuobiao;
            $isedit = 1;
        }else{
            $zb = '118.658935,34.930043';
        }
        $this->assign('zb',$zb);
        $this->assign('isedit',$isedit);
        $this->display('addzb');
    }
    /**
     * 添加应急救援物资坐标
     */
    public function add_rescue_zb(){
        $zuobiao = I('zuobiao');
        $gps = M('emergency_rescue')->where(['position'=>$zuobiao])->find();
        if ($zuobiao) {
            $zb = $zuobiao;
            $isedit = 1;
        }else{
            $zb = '118.658935,34.930043';
        }
        $this->assign('zb',$zb);
        $this->assign('isedit',$isedit);
        $this->display('addzb');
    }
    /**
     * 查看应急救援物资坐标
     */
    public function display_position(){
        $zuobiao = I('zuobiao');
        $isedit = 1;
        $this->assign('isedit',$isedit);
        $this->assign('zb',$zuobiao);
        $this->display();
    }
    /**
     * 查看企业坐标
     */
    public function company_position(){
        $zuobiao = I('zuobiao');
        $isedit = 1;
        $this->assign('isedit',$isedit);
        $this->assign('zb',$zuobiao);
        $this->display();
    }
    //坐标
    public function addzuobiao(){
        $zb = '118.658935,34.930043';
        $zuob = M('emergency_rescue')->select();
        $zuobiao = [];
        foreach ($zuob as $key => $value) {
            //$position = M('emergency_rescue')->field('position')->find();
            array_push($zuobiao,$zuob[$key]['lng']);
            array_push($zuobiao,$zuob[$key]['lat']);
            array_push($zuobiao,$zuob[$key]['name']);
            array_push($zuobiao,$zuob[$key]['location']);
        }
        // print_r($zuobiao);
        // echo '<pre>';
        $chunk_result = array_chunk($zuobiao, 4);
        // print_r($chunk_result);
        // echo '<pre>';
        $zuobiao = json_encode($chunk_result);
        // print_r($zuobiao);
        $this->assign('zb',$zb);
        $this->assign('zuobiao',$zuobiao);
        $this->display();

    }
    //企业坐标
    public function company_zuobiao(){
        $name = I('name');
        $type = I('type');
        $zb = '118.658935,34.930043';
        if(empty($type)){
            $zuob = M('company')->select();
        }else{
            $zuob = M('company')->where(['type'=>$type])->select();
        }
        if(empty($name)){
            $zuob = M('company')->select();
        }else{
            $where['name'] = array('like','%'.$name.'%');
            $zuob = M('company')->where($where)->select();
        }

        $type = M('type_list')->select();
        $zuobiao = [];
        foreach ($zuob as $key => $value) {
            //$position = M('emergency_rescue')->field('position')->find();
            array_push($zuobiao,$zuob[$key]['lng']);
            array_push($zuobiao,$zuob[$key]['lat']);
            array_push($zuobiao,$zuob[$key]['name']);
            array_push($zuobiao,$zuob[$key]['address']);
            array_push($zuobiao,$zuob[$key]['danger_a']);
            array_push($zuobiao,$zuob[$key]['danger_b']);
            array_push($zuobiao,$zuob[$key]['danger_c']);
            array_push($zuobiao,$zuob[$key]['is_check_train']);
            array_push($zuobiao,$zuob[$key]['is_check_drill']);
            array_push($zuobiao,$zuob[$key]['is_check_trouble']);
            
        }
        $chunk_result = array_chunk($zuobiao, 10);
        $zuobiao = json_encode($chunk_result);
        $this->assign('type',$type);
        $this->assign('zuobiao',$zuobiao);
        $this->assign('name',$name);
        $this->display();
    }
    /**
     * 添加公司
     */
    public function add_company()
    {
        if (IS_POST) {
            $name = I('post.name');
            $position = I('post.gps_position');
            $explode = explode(',',$position);
            $data = [
                'name' => $name,
                'code' => I('post.code'),
                'capital' => I('post.capital'),
                'address' => I('post.address'),
                'gps_position' =>$position,
                'lng' => $explode[0],
                'lat' => $explode[1],
                'type' => I('post.type'),
                'person_liable' => I('post.person_liable'),
                'liable_phone' => I('post.liable_phone'),
                'safety_officer' => I('post.safety_officer'),
                'safety_officer_phone' => I('post.safety_officer_phone'),
                'safety_officer_photo' => I('post.safety_officer_photo'),
                'account' => I('post.account'),
                'password' => md5(I('post.password')),
                'status' => 1,
                // 'danger_a' => I('post.danger_a'),
                // 'danger_b' => I('post.danger_b'),
                // 'danger_c' => I('post.danger_c'),
                // 'other' => I('post.other'),
                'create_time' => time(),
            ];
            $res = M('Company')->add($data);

            $userData['username'] = $data['person_liable'];
            $userData['phone'] = $data['account'];
            $userData['password'] = $data['password'];
            $userData['j_push_id'] = 0;
            $userData['level_id'] = 5;
            $userData['role_id'] = 2;
            $userData['company_id'] = $res;

            $userM = M('User');
            $resultUser = $userM->add($userData);
            if (empty($res)) {
                $this->error('服务器繁忙，请稍后再试！');
            }
            $this->success('添加成功', U('Position/company_list'));
        }
        $type = M('type_list')->select();

        $this->assign('type',$type);
        $this->assign('name',$name);
        $this->display('add_company');
    }

    /**
     * 编辑公司
     */
    public function edit_company()
    {
        if (IS_POST) {
            $id = I('post.id');
            $name = I('post.name');
            $position = I('post.gps_position');
            $explode = explode(',',$position);
            $data = [
                'name' => $name,
                'code' => I('post.code'),
                'capital' => I('post.capital'),
                'address' => I('post.address'),
                'gps_position' =>$position,
                'lng' => $explode[0],
                'lat' => $explode[1],
                'type' => I('post.type'),
                'person_liable' => I('post.person_liable'),
                'liable_phone' => I('post.liable_phone'),
                'safety_officer' => I('post.safety_officer'),
                'safety_officer_phone' => I('post.safety_officer_phone'),
                'safety_officer_photo' => I('post.safety_officer_photo'),
                'danger_a' => I('post.danger_a'),
                'danger_b' => I('post.danger_b'),
                'danger_c' => I('post.danger_c'),
                'other' => I('other'),
                'create_time' => time(),

            ];
            $res = M('Company')->where(['id' => $id])->save($data);
            if (empty($res)) {
                $this->error('保存数据出错。');
            }
            $this->success('保存成功。', U('Position/company_list'));
        }
        $type = M('type_list')->select();
        $this->assign('type',$type);
        $id = I('id');
        $info = M('Company')->where(['id' => $id])->find();
        $this->assign('info', $info);
        $this->display('edit_company');
    }

    /**
     * 删除公司
     */
    public function del_company()
    {   
        if (IS_AJAX) {
            $id = I('id');
            M('Company')->where(['id' => $id])->delete();
            $this->success('删除成功', U('Position/company_list'));
        }
    }

    /**
     * 部门列表
     */
    public function part_list()
    {
        $count = M('part')->count();
        $page = getpage($count,15);
        $show = $page->show();
        $list = M('part')->order('id desc')->limit($page->firstRow.','.$page->listRows)->select();
        $company = M('company')->select();
        $this->assign('list', $list);
        $this->assign('company',$company);
        $this->assign('count',$count);
        $this->assign('page',$show);
        $this->display('part_list');
    }

    /**
     * 添加部门
     */
    public function add_part()
    {
        if (IS_POST) {
            $name = I('post.name');
            $company_id = I('post.company_id');
            $data = [
                'name' => $name,
                'company_id' => $company_id,
                'status' => 1,
                'create_time' => date('Y-m-d H:i:s', time()),
            ];
            $res = M('Part')->add($data);
            if (empty($res)) {
                $this->error('服务器繁忙，请稍后再试！');
            }
            $this->success('添加成功', U('Position/part_list'));
        }
        $company = M('Company')->where(['status' => 1])->select();
        $this->assign('company', $company);
        $this->display('add_part');
    }

    /**
     * 编辑部门
     */
    public function edit_part()
    {
        if (IS_POST) {
            $id = I('post.id');
            $name = I('post.name');
            $data = [
                'name' => $name,
            ];
            $res = M('Part')->where(['id' => $id])->save($data);
            if (empty($res)) {
                $this->error('保存数据出错。');
            }
            $this->success('保存成功。', U('Position/part_list'));
        }
        $id = I('id');
        $info = M('Part')->where(['id' => $id])->find();
        $company = M('Company')->where(['status' => 1])->select();
        $this->assign('info', $info);
        $this->assign('company',$company);
        $this->display('edit_part');
    }

    /**
     * 删除部门
     */
    public function del_part()
    {
        if(IS_AJAX){
            $id = I('id');
            M('Part')->where(['id' => $id])->delete();
            $this->success('删除成功', U('Position/part_list'));   
        }
        
    }

    /**
     * 部门列表
     */
    public function workshop_list()
    {
        $list = $this->lists('Workshop', 'status = 1', 'id desc');
        if (!empty($list)) {
            foreach ($list as $k => $v) {
                $list[$k]['part_name'] = M('Part')->where(['id' => $v['part_id']])->getField('name');
            }
        }
        $this->assign('list', $list);

        $Data = M('Workshop'); // 实例化Data数据模型
        $count   = $Data->where( 'status = 1')->count();
        $p = getpage($count,20);
        $this->assign('page', $p->show()); // 赋值分页输出
        $this->display('workshop_list');
    }

    /**
     * 添加部门
     */
    public function add_workshop()
    {
        if (IS_POST) {
            $name = I('post.name');
            $part_id = I('post.part_id');
            $data = [
                'name' => $name,
                'part_id' => $part_id,
                'status' => 1,
                'create_time' => date('Y-m-d H:i:s', time()),
            ];
            $res = M('Workshop')->add($data);
            if (empty($res)) {
                $this->error('服务器繁忙，请稍后再试！');
            }
            $this->success('添加成功', U('Position/workshop_list'));
        }
        $part = M('Part')->where(['status' => 1])->select();
        $this->assign('part', $part);
        $this->display('add_workshop');
    }

    /**
     * 编辑部门
     */
    public function edit_workshop()
    {
        if (IS_POST) {
            $id = I('post.id');
            $part_id = I('post.part_id');
            $name = I('post.name');
            $data = [
                'part_id' => $part_id,
                'name' => $name,
            ];
            $res = M('Workshop')->where(['id' => $id])->save($data);
            
            $this->success('保存成功。', U('Position/workshop_list'));
        }
        $id = I('id');
        $info = M('Workshop')->where(['id' => $id])->find();
        $part = M('Part')->where(['status' => 1])->select();
        $this->assign('part', $part);
        $this->assign('info', $info);
        $this->display('edit_workshop');
    }

    /**
     * 删除部门
     */
    public function del_workshop()
    {
        if(IS_AJAX){
            $id = I('id');
            M('Workshop')->where(['id' => $id])->setField('status', 0);
            $this->success('删除成功', U('Position/workshop_list'));   
        }
        
    }

    /**
     * 部门列表
     */
    public function group_list()
    {
        $list = $this->lists('Group', 'status = 1', 'id desc');
        if (!empty($list)) {
            foreach ($list as $k => $v) {
                $list[$k]['workshop_name'] = M('Workshop')->where(['id' => $v['workshop_id']])->getField('name');
            }
        }
        $this->assign('list', $list);

        $Data = M('Group'); // 实例化Data数据模型
        $count   = $Data->where( 'status = 1')->count();
        $p = getpage($count,20);
        $this->assign('page', $p->show()); // 赋值分页输出

        $this->display('group_list');
    }

    /**
     * 添加部门
     */
    public function add_group()
    {
        if (IS_POST) {
            $name = I('post.name');
            $workshop_id = I('post.workshop_id');
            $data = [
                'name' => $name,
                'workshop_id' => $workshop_id,
                'status' => 1,
                'create_time' => date('Y-m-d H:i:s', time()),
            ];
            $res = M('Group')->add($data);
            if (empty($res)) {
                $this->error('服务器繁忙，请稍后再试！');
            }
            $this->success('添加成功', U('Position/group_list'));
        }
        $workshop = M('Workshop')->where(['status' => 1])->select();
        $this->assign('workshop', $workshop);
        $this->display('add_group');
    }

    /**
     * 编辑部门
     */
    public function edit_group()
    {
        if (IS_POST) {
            $id = I('post.id');
            $name = I('post.name');
            $workshop_id = I('post.workshop_id');
            $data = [
                'workshop_id' => $workshop_id,
                'name' => $name,
            ];
            $res = M('Group')->where(['id' => $id])->save($data);
            $this->success('保存成功。', U('Position/group_list'));
        }
        $id = I('id');
        $info = M('Group')->where(['id' => $id])->find();
        $workshop = M('Workshop')->where(['status' => 1])->select();
        $this->assign('workshop', $workshop);
        $this->assign('info', $info);
        $this->display('edit_group');
    }

    /**
     * 删除部门
     */
    public function del_group()
    {
        if(IS_AJAX){
            $id = I('id');
            M('Group')->where(['id' => $id])->setField('status', 0);
            $this->success('删除成功', U('Position/group_list'));
        }
        
    }
    /**
     * 行业类别列表
     */
    public function industry(){
        $count = M('hy_type')->count();
        $page = getpage($count,20);
        $show = $page->show();
        $industry = M('hy_type')->limit($page->firstRow.','.$page->listRows)->order('id desc')->select();
        $this->assign('industry',$industry);
        $this->assign('count',$count);
        $this->assign('page',$show);
        $this->display('industry_list');
    }
    /**
     * 企业类别列表
     */
    public function typeList(){
        $count = M('type_list')->order('id desc')->count();
        $page = getpage($count,20);
        $show = $page->show();
        $typeList = M('type_list')->limit($page->firstRow.','.$page->listRows)->order('id desc')->select();
        $this->assign('typeList',$typeList);
        $this->assign('count',$count);
        $this->assign('page',$show);
        $this->display('type_list');
    }
    /**
     * 添加企业类别
     */
    public function add_type(){
        if (IS_POST) {
            $name = I('post.name');
            $data = [
                'name' => $name,
                'create_time' => time(),
            ];
            $res = M('type_list')->add($data);
            if (empty($res)) {
                $this->error('内容不能为空！');
            }
            $this->success('添加成功', U('Position/typeList'));
        }
        $this->display('add_type');
    }
    /**
     * 添加行业类别
     */
    public function add_industry(){
        if(IS_POST){
            $name = I('name');
            $data = [
                'name' => $name,
                'create_time' => time(),
            ];
            $result = M('hy_type')->add($data);
            if(empty($result)){
                $this->error('内容不能为空！');
            }else{
                $this->success('添加成功',U('Position/industry'));
            }
        }
        $this->display();
    }
    /**
     * 修改企业类别
     */
    public function edit_type(){
        if (IS_POST) {
            $id = I('post.id');
            $name = I('post.name');
            $data = [
                'name' => $name,
            ];
            $res = M('type_list')->where(['id' => $id])->save($data);
            $this->success('保存成功。', U('Position/typeList'));
        }
        $id = I('id');
        $info = M('type_list')->where(['id' => $id])->find();
        $this->assign('info', $info);
        $this->display('edit_type');
    }
    /**
     * 修改行业类别
     */
    public function edit_industry(){
        if(IS_POST){
            $id = I('id');
            $data['name'] = I('name');
            $result = M('hy_type')->where(['id' => $id])->save($data);
            $this->success('修改成功',U('Position/industry'));
        }
        $id = I('id');
        $info = M('hy_type')->where(['id' => $id])->find();
        $this->assign('info',$info);
        $this->display();
    }
    /**
     * 删除企业类别
     */
    public function del_type()
    {
        if(IS_AJAX){
            $id = I('id');
            M('type_list')->where(['id' => $id])->delete();
            $this->success('删除成功', U('Position/typeList')); 
        }
        
    }
    /**
     * 删除行业类别
     */
    public function del_industry(){
        if(IS_AJAX){
            $id = I('id');
            M('hy_type')->where(['id' => $id])->delete();
            $this->success('删除成功',U('Position/industry'));
        }
    }
    /**
     * 应急救援物资管理
     */
    public function emergencyRescue(){
        $count = M('emergency_rescue')->count();
        $page = getpage($count,20);
        $show = $page->show();
        $rescue = M('emergency_rescue')->limit($page->firstRow.','.$page->listRows)->order('id desc')->select();
        $this->assign('rescue',$rescue);
        $this->assign('count',$count);
        $this->assign('page',$show);
        $this->display('emergency_rescue_list');
    }
    /**
     * 添加应急救援物资管理
     */
    public function add_rescue(){
        if (IS_POST) {
            $name = I('post.name');
            $position = I('post.position');
            $explode = explode(',', $position);
            $data = [
                'name' => $name,
                'location' => I('location'),
                'position' => $position,
                'lng' => $explode[0],
                'lat' => $explode[1],
                'create_time' => time(),
            ];
            $res = M('emergency_rescue')->add($data);
            if (empty($res)) {
                $this->error('服务器繁忙，请稍后再试！');
            }
            $this->success('添加成功', U('Position/emergencyRescue'));
        }
        $this->display('add_rescue');
    }
    /**
     * 修改应急救援物资
     */
    public function edit_rescue(){
        if (IS_POST) {
            $id = I('post.id');
            $name = I('post.name');
            $position = I('post.position');
            $explode = explode(',', $position);
            $data = [
                'name' => $name,
                'location' => I('location'),
                'position' => $position,
                'lng' => $explode[0],
                'lat' => $explode[1]
            ];
            $res = M('emergency_rescue')->where(['id' => $id])->save($data);
            $this->success('保存成功。', U('Position/emergencyRescue'));
        }
        $id = I('id');
        $info = M('emergency_rescue')->where(['id' => $id])->find();
        $this->assign('info', $info);
        $this->display('edit_rescue');
    }
    /**
     * 删除应急救援物资
     */
    public function del_rescue(){
        if(IS_AJAX){
          $id = I('id');
          M('emergency_rescue')->where(['id'=>$id])->delete();
          $this->success('删除成功',U('Position/emergencyRescue'));  
        }
        
    }
}
